package com.msb.servlet;

import com.msb.entity.User;
import com.msb.mapper.UserMapper;
import com.msb.unitil.DBUnitil;
import org.apache.ibatis.io.Resources;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;

/**
 * @version : 1.0
 * @Author: yangmingsong
 * @Date: 2021/2/7-02-07-23:16
 * @Description: demon
 */
@WebServlet(urlPatterns = "/DoRegist")
public class DoRegist extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        User user = new User();
        String username = request.getParameter("username");
        user.setUsername(username);
        String password = request.getParameter("password");
        user.setPassword(password);
        InputStream inputStream = Resources.getResourceAsStream("mybaties.xml");
        UserMapper mapper = DBUnitil.getsqlsession().getMapper(UserMapper.class);
        User user1 = mapper.selectOne(username);
        DBUnitil.getsqlsession().close();
        if(user1.getUsername()!=null){
            request.setAttribute("msg3","用户名重复");
            request.getRequestDispatcher("regist.jsp").forward(request,response);
        }else{
            Integer i = mapper.insert(user);
            if(i > 0){
                //重定向，实现页面跳转
                response.sendRedirect("login.jsp");
            }
        }

    }
}
